package com.zito.health.controller;

import com.zito.health.common.result.PlatformResult;
import com.zito.health.entity.ItemUndertake;
import com.zito.health.entity.Unit;
import com.zito.health.entity.User;
import com.zito.health.service.ItemUndertakeService;
import com.zito.health.service.UnitService;
import com.zito.health.util.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目申请承担单位表
 */
@Slf4j
@RestController
@RequestMapping("/item/undertake")
public class ItemUndertakeController {

    @Autowired
    private ItemUndertakeService itemUndertakeService;
    @Autowired
    private UnitService unitService;


    @PostMapping("/save")
    public PlatformResult save(ItemUndertake item){
        itemUndertakeService.save(item);
        return PlatformResult.success();
    }

    @PostMapping("/update")
    public PlatformResult update(ItemUndertake item){
        itemUndertakeService.update(item);
        return PlatformResult.success();
    }

    @PostMapping("/query")
    public PlatformResult query(ItemUndertake item){
        return PlatformResult.success(itemUndertakeService.queryOne(item));
    }

    /**
     * 更新并保存按钮（科研人员）
     * @param item
     * @return
     */
    @PostMapping("/updateAndSave")
    public ItemUndertake updateAndSave(ItemUndertake item){
        ItemUndertake itemUndertake = new ItemUndertake();
        User current = CacheUtil.getCacheUser().getUser();
        Unit unit=unitService.queryById(current.getUnitId());

        BeanUtils.copyProperties(unit , itemUndertake);
        itemUndertake.setId(item.getId());
        itemUndertake.setItemId(item.getItemId());
        itemUndertakeService.update(itemUndertake);
        return itemUndertake;
    }


}
